package com.shencai.demo.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shencai.demo.base.common.BaseContext;
import com.shencai.demo.base.entity.SysMenu;
import com.shencai.demo.base.entity.SysRole;
import com.shencai.demo.base.model.SysRoleVO;
import com.shencai.demo.base.service.SysRoleService;
import com.shencai.fw.exception.BaseException;
import com.shencai.fw.web.JsonResult;
import com.shencai.fw.web.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:Wangym
 * @Date: 2022/10/17  17:04
 * @Version 1.0
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 根据当前页码和页面大小查询所有角色信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping
    public JsonResult<Page<SysRole>> listRole(Integer currentPage, Integer pageSize) {
        try {
            Page<SysRole> roles = sysRoleService.listByPage(currentPage, pageSize);
            JsonResult<Page<SysRole>> result = new JsonResult<Page<SysRole>>(true, 200, "查询成功");
            result.setObj(roles);
            return result;
        } catch (Exception exception) {
            throw new RuntimeException("查询异常");
        }

    }

    /**
     * 根据姓名和页码模糊查询所有角色信息
     *
     * @param name
     * @return
     */
    @RequestMapping("/queryByName")
    public JsonResult<Page<SysRole>> queryRoleById(String name, Integer currentPage, Integer pageSize) {
        try {
            Page<SysRole> rolePage = sysRoleService.listSysRoleByNameByPage(name, currentPage, pageSize);
            JsonResult<Page<SysRole>> result = new JsonResult<Page<SysRole>>(true, 200, "查询成功");
            result.setObj(rolePage);
            return result;
        } catch (Exception exception) {
            throw new BaseException("查询异常");
        }
    }


    /**
     * 新增角色信息
     *
     * @param userInfo
     * @param sysRole
     * @return
     */
    @RequestMapping("/save")
    public JsonResult<SysRole> addRole(UserInfo userInfo, @RequestBody SysRole sysRole) {
        JsonResult<SysRole> result = null;
        boolean save = false;
        BaseContext.setCurrentId(Integer.valueOf(userInfo.getUserId()));
        /*sysRole.setCreateUserId(12);*/
        save = sysRoleService.save(sysRole);
        if (save) {
            result = new JsonResult<SysRole>(true, 200, "添加成功");
            result.setObj(sysRole);
        } else {
            result = new JsonResult<SysRole>(false, 200, "添加失败");
        }
        return result;
    }

    /**
     * 根据id删除角色
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public JsonResult deleteRole(UserInfo userInfo, @PathVariable("id") Integer id) {
        try {
            JsonResult result = null;
            boolean remove = false;

            BaseContext.setCurrentId(Integer.valueOf(userInfo.getUserId()));
            remove = sysRoleService.removeById(id);
            if (remove) {
                result = new JsonResult<SysRole>(true, 200, "删除成功");
            } else {
                result = new JsonResult<SysRole>(false, 200, "删除失败");
            }
            return result;
        } catch (Exception exception) {
            throw new BaseException("删除异常，请稍后重试~");
        }
    }


    /**
     * 修改角色信息
     *
     * @param sysRole
     * @return
     */
    @RequestMapping("/update")
    public JsonResult updateRole(UserInfo userInfo, @RequestBody SysRole sysRole) {

        try {
            JsonResult<SysRole> result = null;
            boolean update = false;

            BaseContext.setCurrentId(Integer.valueOf(userInfo.getUserId()));
            update = sysRoleService.updateById(sysRole);
            if (update) {
                result = new JsonResult<SysRole>(true, 200, "修改成功");
            } else {
                result = new JsonResult<SysRole>(false, 200, "修改失败");
            }
            return result;

        } catch (Exception exception) {
            throw new BaseException("修改失败，请稍后重试~");
        }

    }
    /**
     * 根据角色id查询所具有的菜单权限
     *
     * @param id
     * @return
     */
    @RequestMapping("/roleMenu")
    public JsonResult<SysMenu> listMenuByRoleId(Integer id) {


        try {
            List<SysMenu> menus = sysRoleService.listMenuByRoleId(id);
            JsonResult<SysMenu> result = new JsonResult<SysMenu>(true, 200, "查询成功");
            result.setData(menus);
            return result;
        } catch (Exception exception) {
            throw new BaseException("查询异常");
        }
    }
    /**
     * 修改角色权限
     *
     * @param userInfo

     */
    @RequestMapping("/updateRoleMenus")
    public JsonResult updateRoleMenus(UserInfo userInfo, @RequestBody SysRoleVO sysRoleVO) {

        Integer roleId = sysRoleVO.getId();
        List<Integer> menuIds = sysRoleVO.getMenuIds();
        try {
            BaseContext.setCurrentId(Integer.valueOf(userInfo.getUserId()));
            boolean update = sysRoleService.updateRoleMenus(roleId, menuIds);
            if (update) {
                JsonResult result = new JsonResult<>(true, 200, "修改成功");
                return result;
            }
            JsonResult result = new JsonResult<SysRole>(false, 200, "修改失败");
            return result;
        } catch (NumberFormatException e) {
            throw new BaseException("修改错误");
        }
    }

    @RequestMapping("/roleUser")
    public JsonResult<SysRole> listUserPer(){
        try {
            List<SysRole> list = sysRoleService.list();
            JsonResult<SysRole> result = new JsonResult<SysRole>(true,200,"查询成功");
            result.setData(list);
            return result;
        }catch (Exception exception){
            throw new BaseException("查询异常");
        }
    }
}
